//
// Created by Semir on 2020/5/24.
//

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

/**
 * 比较结构体
 * 大顶小根堆
 */
struct cmp {
    bool operator() (const int& a, const int& b) {
        return a < b;
    }
};

vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
    vector<int> res;
    if (input.empty() || k <= 0 || k > input.size()) {
        return res;
    }
    priority_queue<int, vector<int>, cmp> Q;
    for (int i = 0; i < k; i++) {
        Q.push(input[i]);
    }
    for (int i = k; i < input.size(); i++) {
        if (input[i] < Q.top()) {
            Q.pop();
            Q.push(input[i]);
        }
    }
    while (!Q.empty()) {
        res.emplace_back(Q.top());
        Q.pop();
    }
    return res;
}

int main() {
    vector<int> input = {4,5,1,6,2,7,3,8};
    vector<int> res = GetLeastNumbers_Solution(input, 4);
    cout << res.size() << endl;
    return 0;
}